<template>
	<view class="mainpadding2">
		<view class="mainpadding_top ffffff radius">
			<view class="titletext hei_text bold">标题</view>
			<textarea class="textareaclass margin_top hei_text main_size bold" placeholder="请输入标题" v-model="title" />
		</view>
		<view class="mainpadding_top ffffff radius">
			<view class="titletext hei_text bold">描述</view>
			<textarea class="textareaclass margin_top hei_text main_size bold" placeholder="请输入描述" v-model="description" />
		</view>
		<view class="mainpadding_top ffffff radius">
			<view class="titletext hei_text bold">上传图片</view>
			<view class="flexleft flex_wrap">
				<view class="itemimgBox flexcenter margin_top" v-for="(item,index) in images_text" :key="item.id">
					<view class="listleftimg">
						<image class="listleftimg radius" :src="item"
							mode="">
						</image>
						<view class="deleteimg" @click="delindex(index,images,images_text)"></view>
					</view>
				</view>
				<view class="itemimgBox flexcenter margin_top" v-if="images_text.length<4" @click="uploadImg(4,images,images_text)">
					<image class="listleftimg radius" src="../static/image/system/upimg.png" mode=""></image>
				</view>
			</view>
		</view>
		<view class="posibottom mainpadding">
			<view class="submitbtn margin_top5" @click="submit">发布</view>
		</view>
	</view>
</template>

<script>
	const httpRequest = require("@/common/httpRequest.js")
	export default {
		data() {
			return {
				description:"",
				title:"",
				listid:"",
				images:[],
				images_text:[],
			}
		},
		onLoad(options) {
			if(options.id){
				this.listid = options.id
				this.getDetail()
			}
		},
		methods: {
			getDetail(){
				httpRequest.request('/api/community/getform', 'POST', {
					id:this.listid
				}, false, false, true).then(res => {
					this.description = res.data.description
					this.title = res.data.title
					this.images_text = res.data.images_arr
					this.images = res.data.images.split(',')
				}).catch(err => {
					httpRequest.toast(err.msg);
				})
			},
			submit(){
				httpRequest.request('/api/community/publish', 'POST', {
					id:this.listid,
					title:this.title,
					description:this.description,
					images:this.images.join()
				}, false, false, true).then(res => {
					httpRequest.toast(res.msg);
					if(res.code==1){
						setTimeout(()=>{
							uni.navigateBack(1)
						},1000)
					}
				}).catch(err => {
					httpRequest.toast(err.msg);
				})
			},
			uploadImg(value, upImgs, upImgs_text) { //上传图片
				let _this = this;
				let num = Number(value) - upImgs.length;
				if (num <= 0) {
					return false;
				}
				uni.showActionSheet({
					itemList: ['相机', '相册'],
					success: function(res) {
						// 选择相机走0
						if (res.tapIndex == 0) {
							httpRequest.chooseImage(num, ['camera']).then(res => {
								for (var i = 0; i < res.length; i++) {
									_this.uploadImageService(res[i], upImgs, upImgs_text)
								}
							}).catch(err => {});
						}
						// 选择相册走1
						if (res.tapIndex == 1) {
							httpRequest.chooseImage(num, ['album']).then(res => {
								for (var i = 0; i < res.length; i++) {
									_this.uploadImageService(res[i], upImgs, upImgs_text)
								}
							}).catch(err => {});
						}
					},
					fail: function(res) {}
				});
			},
			delindex(index,upImg, upImgs_text){
				upImg.splice(index,1)
				upImgs_text.splice(index,1)
			},
			uploadImageService(url, upImg, upImgs_text) {
				let _this = this;
				httpRequest.uploadFile('/api/common/upload', url).then(res => {
					let image_item = res.fullurl;
					upImg.push(res.url)
					upImgs_text.push(image_item);
				}).catch(err => {});
			},
		}
	}
</script>

<style>

</style>